/**
 * 获取uuid
 */
export function getUUID () {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
    return (c === 'x' ? (Math.random() * 16 | 0) : ('r&0x3' | '0x8')).toString(16)
  })
}

/**
 * 是否有权限
 * @param {*} key
 */
export function isAuth (key) {
  return JSON.parse(sessionStorage.getItem('permissions') || '[]').indexOf(key) !== -1 || false
}

/**
 * 树形数据转换
 * @param {*} data
 * @param {*} id
 * @param {*} pid
 */
export function treeDataTranslate (data, id = 'id', pid = 'parentId') {
  var res = []
  var temp = {}
  for (var i = 0; i < data.length; i++) {
    temp[data[i][id]] = data[i]
  }
  for (var k = 0; k < data.length; k++) {
    if (temp[data[k][pid]] && data[k][id] !== data[k][pid]) {
      if (!temp[data[k][pid]]['children']) {
        temp[data[k][pid]]['children'] = []
      }
      if (!temp[data[k][pid]]['_level']) {
        temp[data[k][pid]]['_level'] = 1
      }
      data[k]['_level'] = temp[data[k][pid]]._level + 1
      temp[data[k][pid]]['children'].push(data[k])
    } else {
      res.push(data[k])
    }
  }
  return res
}

export function urlParamsHasKey (url, key) {
  var str = url.split('?')[1]
  if (!str || str === '') {
    return false
  }
  var hasKey = false
  var str1 = str.split('&')
  str1.map((value, index, array) => {
    if (key === value.split('=')[0]) {
      hasKey = value.split('=')[1]
    }
  })

  return hasKey
}

// utf-8字符串编码
export function utf8Encode (string) {
  string = (string + '').replace(/\r\n/g, '\n').replace(/\r/g, '\n')
  let [utftext, stringl = 0, start, end, n] = ['', 0, 0, 0]
  stringl = string.length
  for (n = 0; n < stringl; n++) {
    let c1 = string.charCodeAt(n)
    let enc = null
    if (c1 < 128) {
      end++
    } else if ((c1 > 127) && (c1 < 2048)) {
      enc = String.fromCharCode((c1 >> 6) | 192, (c1 & 63) | 128)
    } else {
      enc = String.fromCharCode((c1 >> 12) | 224, ((c1 >> 6) & 63) | 128, (c1 & 63) | 128)
    }
    if (enc !== null) {
      if (end > start) {
        utftext += string.substring(start, end)
      }
      utftext += enc
      start = end = n + 1
    }
  }
  if (end > start) {
    utftext += string.substring(start, string.length)
  }
  return utftext
}
// base64编码
export function base64Encode (data) {
  let b64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='
  let [i, ac, enc, tmpArr, o1, o2, o3, h1, h2, h3, h4, bits] = [0, 0, '', []]
  if (!data) {
    return data
  }
  data = utf8Encode(data)
  do {
    o1 = data.charCodeAt(i++)
    o2 = data.charCodeAt(i++)
    o3 = data.charCodeAt(i++)

    bits = o1 << 16 | o2 << 8 | o3
    h1 = bits >> 18 & 0x3f
    h2 = bits >> 12 & 0x3f
    h3 = bits >> 6 & 0x3f
    h4 = bits & 0x3f
    tmpArr[ac++] = b64.charAt(h1) + b64.charAt(h2) + b64.charAt(h3) + b64.charAt(h4)
  } while (i < data.length)

  enc = tmpArr.join('')

  switch (data.length % 3) {
    case 1:
      enc = enc.slice(0, -2) + '=='
      break
    case 2:
      enc = enc.slice(0, -1) + '='
      break
  }

  return enc
}

/**
 *获取页面路径
 *
 * @author long.zhou
 * @date 2018-08-09
 * @export
 * @returns {String}
 */
export function path (name) {
  var path = normalizePath(name)
  return path.toLowerCase()
}

/**
 *路径格式整理
 *
 * @author long.zhou
 * @date 2018-08-09
 * @export
 * @param {String} path
 * @returns {String}
 */
function normalizePath (path) {
  var length = path.length
  if (length > 1 && path.charAt(length - 1) === '/') {
    return path.slice(0, length - 1)
  } else {
    return path
  }
}
